﻿using System;
using System.Collections.Generic;
public class Recipe
{
    public int id;
    public BlockMaterial block;
    public List<ItemStack> Target = new List<ItemStack>();
    public List<ItemStack> Res = new List<ItemStack>();

    public Recipe() { }
    public Recipe(BlockMaterial block)
    {
        this.block = block;
    }
    public Recipe AddTarget(ItemStack t)
    {
        Target.Add(t);
        return this;
    }
    public Recipe AddRes(ItemStack t)
    {
        Res.Add(t);
        return this;
    }
    public void GiveItem(Store store)
    {
        foreach (ItemStack item in Target)
        {
            store.AddItem(item.Copy());
        }
    }
    public bool RemoveRes(Store store)
    {
        Dictionary<String, int> reslist = new Dictionary<String, int>();
        foreach (ItemStack item in Res)
        {
            reslist.Add(item.Name, item.Amount);
        }
        for (int i = 0; i < store.Max; i++)
        {
            ItemStack item = store.GetItemStack(i);
            if (item == null) continue;

            if (reslist.ContainsKey(item.Name))
            {
                if (item.Amount >= reslist[item.Name])
                {
                    store.SubAmount(i, reslist[item.Name]);
                    reslist.Remove(item.Name);
                }
                else
                {
                    reslist[item.Name] -= item.Amount;
                    store.Remove(i);
                }
            }
        }
        //
        if (reslist.Count != 0) return false;
        else return true;
    }
    public bool CanCraft(Store store)
    {
        Dictionary<String, int> reslist = new Dictionary<String, int>();
        for (int i = 0; i < store.Max; i++)
        {
            ItemStack item = store.GetItemStack(i);
            if (item == null) continue;
            foreach (ItemStack ii in Res)
            {
                if (ii.Name == item.Name)
                {
                    if (reslist.ContainsKey(item.Name))
                    {
                        reslist[item.Name] += item.Amount;
                    }
                    else
                    {
                        reslist.Add(item.Name, item.Amount);
                    }
                }
            }
        }

        if (reslist.Count == 0) return false;

        foreach (ItemStack item in Res)
        {
            if (reslist.ContainsKey(item.Name) && reslist[item.Name] >= item.Amount)
            {

            }
            else
            {
                return false;
            }
        }
        return true; ;
    }
    public String getResList()
    {
        String str = "";
        for (int i = 0; i < Res.Count; i++)
        {
            ItemStack item = Res[i];
            if (i != Res.Count - 1)
            {
                str += $"{item.DispalyName}  *  {item.Amount}\n";
            }
            else
            {
                str += $"{item.DispalyName}  *  {item.Amount}";
            }

        }
        return str;
    }
}

public class RecipeItem
{
    int i;
    ItemStack item;
    public RecipeItem(int i, ItemStack item)
    {
        this.i = i;
        this.item = item;
    }
}